package se.bl.support;

import static qj.util.FileUtil.*;

import java.io.Console;
import java.io.File;
import java.io.IOException;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Properties;

import qj.util.FileUtil;
import qj.util.PropertiesUtil;
import qj.util.StringUtil;
import qj.util.ZipUtil;
import qj.util.funct.F1;
import qj.util.funct.P1;

public class BenjaminDeployMain {
	static Properties config = PropertiesUtil.loadPropertiesFromFile("data/defaultConfig.properties");
	static {
		File configFile = new File("config.properties");
		if (configFile.exists()) {
			try {
				config.load(FileUtil.fileInputStream(configFile));
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		
		System.out.println(config);
	}
	
	public static void main(String[] args) {
		
		delete(new File("target/tariff-bl.war"));
		delete(new File("target/ws-client-1.0-SNAPSHOT.jar"));
		
		System.out.print("Unzip tariff-bl.war...");
		File warDir = new File("target/tariff-bl.war");
		ZipUtil.unzip(new File("tariff-bl.war"), warDir);
		System.out.println(" done.");
		
		
		ZipUtil.unzip(new File("target/tariff-bl.war/WEB-INF/lib/ws-client-1.0-SNAPSHOT.jar"),new File("target/ws-client-1.0-SNAPSHOT.jar"));
		new File("target/ws-client-1.0-SNAPSHOT.jar/wsdl").mkdirs();
		writeTariffServiceV1WSwsdl();
		copyFile(new File("data/TariffServiceV1.class"), new File("target/ws-client-1.0-SNAPSHOT.jar/se/arcticgroup/tariff/itswtariff/ws/TariffServiceV1.class"));

		delete(new File("target/tariff-bl.war/WEB-INF/lib/ws-client-1.0-SNAPSHOT.jar"));
		ZipUtil.zip(new File("target/ws-client-1.0-SNAPSHOT.jar"),new File("target/tariff-bl.war/WEB-INF/lib/ws-client-1.0-SNAPSHOT.jar"));
		
		
		final String jdbcUrl = config.getProperty("jdbc.url");
		if (StringUtil.isNotEmpty(jdbcUrl)) {
			F1<String, String> jdbcF = new F1<String, String>() {public String e(String obj) {
				return obj.replaceAll("jdbc:oracle:thin:@\\w+:\\w+:\\w+", jdbcUrl);
			}};
			editFile(new File("target/tariff-bl.war/WEB-INF/classes/jdbc.properties"), jdbcF);
			editFile(new File("target/tariff-bl.war/WEB-INF/classes/applicationContext-resources.xml"), jdbcF);
		}
		
		if ("true".equals(config.getProperty("replace.login.files"))) {
			copy("data/login.jsp", "target/tariff-bl.war");
			copy("data/login.js", "target/tariff-bl.war/scripts");
		}
		
		if ("true".equals(config.getProperty("remove.libs"))) {
			System.out.print("Removing redundant lib files...");
			removeRedundantLibFiles(new File(warDir,"WEB-INF/lib"));
			System.out.println(" done.");
		}
		
		System.out.print("Renaming the source tariff-bl.war file to tariff-bl.war.old...");
		new File("tariff-bl.war").renameTo(new File("tariff-bl.war.old"));
		System.out.println(" done.");

		System.out.print("Create new tariff-bl.war file...");
		ZipUtil.zip(warDir,new File("tariff-bl.war"));
		System.out.println(" done.");
		
//		System.out.println("");
		
	}
	
	private static void writeTariffServiceV1WSwsdl() {

		ZipUtil.copyFile("META-INF/wsdl/TariffServiceV1WS.wsdl", new File("target/tariff-bl.war/WEB-INF/lib/core-1.0-SNAPSHOT.jar"), new File("target/ws-client-1.0-SNAPSHOT.jar/wsdl"));

		editFile(new File("target/ws-client-1.0-SNAPSHOT.jar/wsdl/TariffServiceV1WS.wsdl"), new F1<String,String>() {public String e(String obj) {
			return obj.replaceAll("(<soap:address location=['\"]http:)//[^:]+:.+?/", "$1//" + config.getProperty("wsdl.location") + "/");
		}});
	}
	static HashSet<String> libs = new HashSet<String>(Arrays.asList(readFileToString("data/lib.txt").split("\r?\n")));
	private static void removeRedundantLibFiles(File file) {
		eachFile(file, new P1<File>() {public void e(File file) {
			if (libs.contains(file.getName())) {
				file.delete();
			}
		}} );
	}
}
